package com.dyoud.api.message.controller;

import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.dyoud.api.ReturnInfo;
import com.dyoud.api.message.dto.MessageDto;
import com.dyoud.api.message.service.MessageApiService;
import com.dyoud.core.ResponseBean;
import com.dyoud.message.api.model.ResultMessage;
/**
 * 消息模块API
 * @author Harry
 * @date 2018/03/01
 *
 */
@RestController
@RequestMapping("message")
public class MessageController {

    @Resource
    private MessageApiService messageApiService;

    /**
     * 新增message
     * @param dto
     * @return
     */
    @RequestMapping(value="/",method=RequestMethod.POST)
    public ResponseBean addMessageModify(MessageDto dto) {
    	
    	ResponseBean res = null;
    	if (StringUtils.isEmpty(dto.getContent())) {
			
    		res = new ResponseBean().fail(ReturnInfo.MESSAGE_CONTENT_NULL_CODE, ReturnInfo.MESSAGE_CONTENT_NULL_MESSAGE);
    		return res;
		}
    	/*if (StringUtils.isEmpty(dto.getPhone())) {
    		res = new ResponseBean().fail(ReturnInfo.MESSAGE_PHONE_NULL_CODE, ReturnInfo.MESSAGE_PHONE_NULL_MESSAGE);
    		return res;
    	}*/
    	if (messageApiService.addUserCommentModify(dto)) {
    		res = new ResponseBean().success();
		}else {
			res = new ResponseBean().fail(ReturnInfo.MESSAGE_ADD_FAIL_CODE, ReturnInfo.MESSAGE_ADD_FAIL_MESSAGE);
		}
    	return res;
    }
    
    /**
     * 获取消息列表
     * @param userType
     * @param type
     * @param page
     * @return
     */
    @RequestMapping(value="/listMessage", method=RequestMethod.GET)
    public ResponseBean listMessage(@RequestParam int userType,@RequestParam int type,@RequestParam(defaultValue="1") int pageNo,@RequestParam long userId) {
    	
    	Map<String, Object> data = messageApiService.listMessage(userType,type,pageNo,userId);
    	return new ResponseBean().success(data);
    }
    
    /**
     * 获取未读消息数量
     * @return
     */
    @RequestMapping(value="/countUnreadMsg")
    public ResponseBean countUnreadMsg(@RequestParam int userType,@RequestParam long userId) {
    	
    	ResponseBean res = null;
    	if (userId == 0) {
    		res = new ResponseBean().fail("userId can't be 0.");
			return res;
		}
    	List<Map<String, Object>> countMsg = messageApiService.countUnreadMsg(userType,userId);
    	res = new ResponseBean().success(countMsg);
    	return res;
    }
    
    /**
     * 将消息设为已读
     * @return
     */
    @RequestMapping(value="/setToRead")
    public ResponseBean setToRead(@RequestParam long msgId) {
    	
    	boolean result = messageApiService.setToRead(msgId);
    	
    	return result ? new ResponseBean().success() : new ResponseBean().fail("failed");
    }
    
    
    /**
	 * 查询系统消息(0:店铺的系统消息.1:用户的系统消息)
	 *
	 *@param request
     *@return
	 */
    @RequestMapping(value = "userType", method = RequestMethod.GET)
    @ResponseBody
    public ResponseBean selectSystemMessage( MessageDto dto) {
    	List<ResultMessage> selectSystemMessage = messageApiService.selectSystemMessage(dto);
        return new ResponseBean().success(selectSystemMessage);
    };
    
    /**
	 * 查看收益权
	 * 
	 *@param request
     * @return	
	 */
    @RequestMapping(value = "userIdIncome", method = RequestMethod.GET)
    @ResponseBody
    public ResponseBean selectIncome(@Validated MessageDto dto) {
    	return new ResponseBean().success(messageApiService.selectIncome(dto));
    };
    
    /**
	 * 查看留言回复消息
	 * 
	 *@param request
     * @return
	 */
    @RequestMapping(value = "userIdReply", method = RequestMethod.GET)
    @ResponseBody
    public ResponseBean selectReply(@Validated MessageDto dto) {
    	return new ResponseBean().success(messageApiService.selectReply(dto));
    };
    
    /**
	 * 根据唯一key查询消息
	 * 
	 *@param request
     * @return	 *
	 */
    @RequestMapping(value = "onlyKey", method = RequestMethod.GET)
    public ResponseBean selectByOnlyKey(@Validated MessageDto dto) {
    	return new ResponseBean().success(messageApiService.selectByOnlyKey(dto));
    };
    
    /**
     * 根据userId或者shopId
     * 
     *@param request
     * @return	 *
     */
    @RequestMapping(value = "Or", method = RequestMethod.GET)
    public ResponseBean selectByUserIdOrShopId(@Validated MessageDto dto) {
    	return new ResponseBean().success(messageApiService.selectByUserIdOrShopId(dto));
    };

}
